{(function (window){
    function Promise(excutor){
        var _this = this
        this.status = "pending"
        this.data = undefined 
        this.callbacks = []


        function resolve(result){
            if(_this.status !== "pending"){
                return
            }
            _this.status = "fulfilled"
            _this.data = result
            if(_this.callbacks.length > 0){
                _this.callbacks.forEach(function(item){
                    setTimeout(function (){
                        item.fnResolved(result)
                    })
                })
            }
        }


        function reject(result){
            if(_this.status !== "pending"){
                return
            }
            _this.status = "re(jected"
            _this.data = result
            if(_this.callbacks.length > 0){
                _this.callbacks.forEach(function(item){
                    setTimeout(function (){
                        item.fnRejected(result)
                    })
                })
            }
        }
        try{
            excutor(resolve,reject)
        }catch(err){
            reject(err)
        }
    }
    Promise.prototype.then = function (fnResolved,fnRejected){
        var _this = this
        return new Promise((resolve,reject)=>{
            if(_this.status === "fulfilled"){
                try{
                    setTimeout(function (){
                        var res = fnResolved(_this.data)
                        if(res instanceof Promise){
                            res.then(resolve,reject)
                        }else{
                            resolve(res)
                        }
                    })
                }catch(err){
                    setTimeout(function (){
                        var res = fnRejected(_this.data)
                    }) 
                }
            } else if(_this.status === "rejected"){
                try{
                    setTimeout(function (){
                        var res = fnRejected(_this.data)
                        if(res instanceof Promise){
                            res.then(resolve,reject)
                        }else{
                            resolve(res)
                        }
                    }) 
                }catch(err){
                    setTimeout(function (){
                        var res = fnRejected(_this.data)
                    }) 
                }
            } else{
                _this.callbacks.push({
                    "fnResolved": function (){
                        try{
                            setTimeout(function (){
                                var res = fnResolved(_this.data)
                                if(res instanceof Promise){
                                    res.then(resolve,reject)
                                }else{
                                    resolve(res)
                                }
                            })
                        }catch(err){
                            reject(err)
                        }
                    },
                    "fnRejected": function (){
                        try{
                            setTimeout(function (){
                                var res = fnRejected(_this.data)
                                if(res instanceof Promise){
                                    res.then(resolve,reject)
                                }else{
                                    resolve(res)
                                }
                            })
                        }catch(err){
                            reject(err)
                        }
                    }
                })
            }
        })
    }
    window.Promise = Promise
})(window)}